import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'

Vue.use(Router)

/**
 * 路由配置说明：
 * hidden: true                   设置为true时不会显示在侧边栏
 * permission: 'xxx'              设置该路由的权限标识
 * meta : {
    title: 'title'               设置该路由在侧边栏和面包屑中展示的名字
    icon: 'icon-name'            设置该路由的图标
   }
 */

export const routes = [
  // 基础路由
  {
    path: '/login',
    component: () => import('@/views/Login/index'),
    hidden: true // 不在侧边栏显示
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 主页路由
  {
    path: '/',
    component: Layout,
    redirect: '/workbench' // 重定向到工作台
  },
  {
    path: '/workbench',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Workbench/index'),
      meta: { title: '工作台', icon: 'el-icon-data-board' }
    }]
  },

  // 园区管理模块
  {
    path: '/park',
    component: Layout,
    permission: 'park',
    meta: { title: '园区管理', icon: 'el-icon-office-building' },
    children: [{
      path: 'building',
      permission: 'park:building',
      meta: { title: '楼宇管理' },
      component: () => import('@/views/Park/Building/index')
    },
    {
      path: 'enterprise',
      permission: 'park:enterprise',
      meta: { title: '企业管理' },
      component: () => import('@/views/Park/Enterprise/index')
    }]
  },

  // 行车管理模块
  {
    path: '/parking',
    component: Layout,
    permission: 'parking',
    meta: { title: '行车管理', icon: 'el-icon-guide' },
    children: [{
      path: 'area',
      permission: 'parking:area',
      component: () => import('@/views/Car/CarArea'),
      meta: { title: '区域管理' }
    }, {
      path: 'card',
      permission: 'parking:card',
      component: () => import('@/views/Car/CarCard'),
      meta: { title: '月卡管理' }
    }, {
      path: 'pay',
      permission: 'parking:payment',
      component: () => import('@/views/Car/CarPay'),
      meta: { title: '停车缴费管理' }
    },
    {
      path: 'rule',
      permission: 'parking:rule',
      component: () => import('@/views/Car/CarRule'),
      meta: { title: '计费规则管理' }
    }]
  },

  // 一体杆管理模块
  {
    path: '/pole',
    component: Layout,
    permission: 'pole',
    meta: { title: '一体杆管理', icon: 'el-icon-refrigerator' },
    children: [{
      path: 'info',
      permission: 'pole:info',
      component: () => import('@/views/Rod/RodManage'),
      meta: { title: '一体杆管理' }
    }, {
      path: 'waring',
      permission: 'pole:warning',
      component: () => import('@/views/Rod/RodWarn'),
      meta: { title: '告警记录' }
    }]
  },

  // 系统管理模块
  {
    path: '/sys',
    component: Layout,
    permission: 'sys',
    meta: { title: '系统管理', icon: 'el-icon-setting' },
    children: [{
      path: 'role',
      permission: 'sys:role',
      component: () => import('@/views/System/Role/index'),
      meta: { title: '角色管理' }
    }, {
      path: 'user',
      permission: 'sys:user',
      component: () => import('@/views/System/Employee/index'),
      meta: { title: '员工管理' }
    }]
  },

  // 独立页面路由
  {
    path: '/enterpriseAdd',
    meta: { title: '添加企业' },
    component: () => import('@/views/Park/Enterprise/Add.vue'),
    hidden: true
  },
  {
    path: '/parking/card/add',
    component: () => import('@/views/Car/CarCard/CardManage.vue'),
    hidden: true
  },
  {
    path: '/parking/card/edit/:id?',
    component: () => import('@/views/Car/CarCard/CardManage.vue'),
    hidden: true
  },
  {
    path: '/exterpriseDetail/:id?',
    name: 'EnterpriseDetail',
    component: () => import('@/views/Park/Enterprise/Detail'),
    hidden: true
  },
  {
    path: '/roleAdd',
    component: () => import('@/views/System/Role/RoleAdd'),
    hidden: true
  }
]

/**
 * 创建路由实例
 */
const createRouter = () => new Router({
  mode: 'history', // 使用 history 模式
  scrollBehavior: () => ({ y: 0 }), // 切换路由时滚动到页面顶部
  routes: routes
})

// 创建路由实例
const router = createRouter()

/**
 * 重置路由方法
 * 用于在退出登录时清空路由权限
 */
export function resetRouter() {
  try {
    const newRouter = createRouter()
    // 重置路由配置
    router.matcher = newRouter.matcher
    console.log('路由重置成功')
  } catch (error) {
    console.error('路由重置失败:', error)
  }
}

// 导出路由实例
export default router
